﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using RHBOQry = Texfina.BOQry.Rh;
using System.Text;

namespace RH
{
   public partial class PersonalBnd : SearchPage
   {

      public const int _GRDLIST_IDPERSONAL = 3;
      public const int _GRDLIST_IDESTADO = 6;
      public const int _GRDLIST_STANULADO = 8;


      protected void Page_Load(object sender, EventArgs e)
      {

         this.LoadInitialSettings();
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }

         this.LoadData();
         SearchData();

      }

      public override void RegisterGeneralScripts()
      {
         StringBuilder sb = new StringBuilder();

         sb.AppendLine("var ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value='';");
         sb.AppendLine("var ctl = document.getElementById('" + this.txtdsPersonal.ClientID + "');");
         sb.AppendLine("ctl.value='';");
         sb.AppendLine("var ctl = document.getElementById('" + this.txtnuDocIden.ClientID + "');");
         sb.AppendLine("ctl.value='';");
         bttClear.Attributes.Add("onclick", sb.ToString());

      }


      public override void LoadInitialSettings()
      {
         Label lbl = (Label)this.Master.FindControl("lblTitle");
         lbl.Text = "Personal";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void SearchData()
      {

         EPersonalBandeja objE = new EPersonalBandeja();

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         if (this.txtidPersonal.Text == "")
            objE.IdPersonal = null;
         else
            objE.IdPersonal = this.txtidPersonal.Text;

         if (this.txtnuDocIden.Text == "")
            objE.NuDocIden = null;
         else
            objE.NuDocIden = this.txtnuDocIden.Text;

         if (this.txtdsPersonal.Text == "")
            objE.DsApellNom = null;
         else
            objE.DsApellNom = this.txtdsPersonal.Text;

         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Personal));
         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         String strQS = "";

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
               strQS = "wfPersonalMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&IdPersonal=";
               break;
         }
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA = FormEditAction.AddNew;

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
            case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
            case ConstGridRowCommand.GRIDPREVIEW:
               //this.PrintReport(Convert.ToInt32(e.CommandArgument));
               return;
         }
         String strQS = "wfPersonalMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&IdPersonal=" + dr["idPersonal"].ToString();
         Response.Redirect(strQS);

      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            String strRI = e.Row.RowIndex.ToString();

            ImageButton bttedit = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDEDIT);
            ImageButton bttdelete = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDDELETE);
            ImageButton bttHist = (ImageButton)e.Row.FindControl("grdBttCmdHist");

            bttedit.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDEDIT + "','" + strRI + "');";
            bttdelete.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDDELETE + "','" + strRI + "');";
            bttHist.OnClientClick = "javascript:return validateGridCommands('CmdHist','" + strRI + "');";


            if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            {
               e.Row.ForeColor = System.Drawing.Color.Red;
               bttedit.ImageUrl = "../Images/Icons/Disabled/Edit.png";
               bttdelete.ImageUrl = "../Images/Icons/Disabled/Delete.png";
            }
            else
            {
               if (e.Row.Cells[_GRDLIST_IDESTADO].Text == ConstEstadoRegPersonal.PENDIENTE)
               { e.Row.ForeColor = System.Drawing.Color.Green; }

            }

         }

      }

      protected void bttOk_Click(object sender, EventArgs e)
      {
         this.SearchData();
      }
   }
}